package com.gitee.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gitee.exception.ServiceException;
import com.gitee.mapper.UserMapper;
import com.gitee.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: TODO
 * @author: chennl
 * @date: 2021/12/29
 * @version: 1.0
 */
@RestController
public class DemoController {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 注册用户 默认开启白名单
     * @param user
     */
    @PostMapping("/reg")
    public Boolean reg(@RequestBody User user) {
        User origin = userMapper.selectOne(new QueryWrapper<User>().eq(User.USERNAME, user.getUsername()));
        if(null != origin){
            throw new ServiceException("用户已经存在");
        }
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        return userMapper.insert(user) > 0;
    }

}
